package com.weixin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weixin.entity.Employee;
import com.weixin.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private IEmployeeService employeeService;
    @RequestMapping("/login")
    public String login(Employee EE, HttpSession session, HttpServletRequest request){
        //条件构造
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ename",EE.getEname()).eq("password",EE.getPassword());
        //查询数据句库
        Employee employee = employeeService.getOne(queryWrapper);
        if (employee!=null){
            //登陆成功,把employee存到session作用域

            session.setAttribute("employee",employee);
        }else if((EE.getEname().equals("ename")&&EE.getPassword().equals("password"))==false){
            session.setAttribute("error","员工姓名与密码不匹配，请重新登陆");
        }


        return "redirect:/";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        //清空session空间数据
        session.invalidate();

        return "redirect:/";
    }
}
